﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using PocketEngine.Engine;
using PocketEngine.Scripting;

namespace PocketStudio
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            
            InitializeComponent();
            CheckContentPath();
            ScanActiveContent();
            Point wp = Properties.Settings.Default.WindowPos;
            if (wp.X == 0)
            {
                if (wp.Y == 0)
                {
                    Properties.Settings.Default["WindowPos"] = this.Location;
                    Properties.Settings.Default["WindowSize"] = this.Size;
                }
            }
            /*
            WorldView.InitEngine();
            pEngine.Host.HostControl = (Control)this;
            */
            RunFolder(Path.GetFullPath("scripts/startup/"));


 
        }
        public void LogMsg(string msg)
        {
            DevLog.Text = DevLog.Text+"\n"+msg+"\n";
        }
        public void LogErr(string err,string location)
        {
            DevLog.Text = DevLog.Text+"\nError:"+err+"\nLocation:"+location+"\n";
        }
        public void ClearLog()
        {
            DevLog.Clear();
        }
        public void RunFolder(string path)
        {
            return;
            DirectoryInfo di = new DirectoryInfo(path);
            foreach (var f in di.GetFiles())
            {
               LogMsg("Compiling:"+f.Name);
                pScript tmp = new pScript(f.FullName);
                LogMsg("Executing...");
                tmp.Run("Execute", this);
                LogMsg("Executed.");
            }
        }
        public void ScanActiveContent()
        {

        }
        public string ContentPath
        {
            get
            {
                return mContentPath;
                
            }
            set
            {
                mContentPath = value;
                ContentPathLabel.Text = "Content->"+value;
                Properties.Settings.Default["ContentPath"] = value;
            }
        }
        private string mContentPath = string.Empty;
        private void CheckContentPath()
        {
            string cp = Properties.Settings.Default.ContentPath;
            if (cp == null || cp == string.Empty || cp == "" || cp.Length<2)
            {
                ContentPath = RequestDir("Select a Content folder from one of your PocketEngine projects. PocketBattle example game for example.");
                return;
            }
            ContentPath = cp;


        }
        public string RequestDir(string info)
        {
            ContentRequest.Description = info;
            ContentRequest.ShowDialog();
            string ret = ContentRequest.SelectedPath;
            if (ret == null) return string.Empty;
            return ret;
        }
        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void splitContainer3_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void changeContentPathToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            CheckContentPath();
        }

        private void invisibleFlow1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Save();
        }
    }
}
